<!-- 即将保养 -->
<template>
  <a-modal
    :visible="visible"
    @cancel="onClose"
    :title="pageTlt"
    :mask-closable="false"
    :align-center="false"
    class="modal-dialog-wrapper"
    unmount-on-close
    top="30px"
    width="85vw"
    okText="确认"
  >
    <div style="max-height: 85vh">
      <list-searcher :searchList="searchList" v-model:searchForm="queryParam" @loadData="loadData" />
      <table-index :table="table" :rowKey="`id`" @loadData="loadData">
        <template #carInfo="{ record }">
          <div class="car-msg">
            <div class="car-msg-left">
              <div class="col-text col-lic"> 车牌：{{ record.licenseNo || '-' }} </div>
              <div class="col-text"> 车型：{{ record.modelName || '-' }} </div>
            </div>
          </div>
        </template>
        <template #detailNameList="{ record }">
          <div class="car-msg">
            <div class="car-msg-left">
              <div class="col-text" v-for="item in record" :key="item"> {{ item || '-' }} </div>
            </div>
          </div>
        </template>
      </table-index>
    </div>
    <template #footer>
      <a-button @click="onClose">关闭</a-button>
    </template>
  </a-modal>
</template>

<script setup name="carmodal">
  import { ref, reactive, computed } from 'vue'
  import { getMaintenanceList, getMaintenanceExport } from '@/api/workplace.js'
  import { useExport } from '@/hooks/list'
  import { useTable, useFetchTableDataApi } from '@/hooks/table'
  import useNavigate from '@/hooks/useNavigate'
  import dayjs from 'dayjs'

  const { navigateTo } = useNavigate()
  //查询条件
  let queryParam = reactive({})
  const searchList = computed(() => {
    return [
      {
        label: '车辆当前所属门店',
        prop: 'carSiteId',
        type: 'remote-select',
        attrs: {
          type: 'site_listBystaff',
          popupContainer: '.modal-dialog-wrapper .remote-select-wrapper',
          params: {
            oldId: queryParam.carSiteId,
            idList: [queryParam.carId],
          },
        },
        change: (val, option) => {
          if (val) {
            queryParam.carId = ''
          }
        },
      },
      {
        label: '车牌号',
        prop: 'carId',
        type: 'remote-select',
        attrs: {
          type: 'car_licenseno_list',
          popupContainer: '.modal-dialog-wrapper .remote-select-wrapper',
          params: {
            carSiteId: queryParam.carSiteId,
          },
        },
      },
    ]
  })
  // 待处理 - table 配置
  const table = useTable({
    queryParam: queryParam,
    tableHeight: 'calc(100vh - 400px)',
    columns: [
      { title: '车辆信息', dataIndex: 'carInfo', slotName: 'carInfo', width: 240, fixed: 'left' },
      { title: '车辆当前所属门店', dataIndex: 'carSiteName', width: 150 },
      { title: '车辆当前里程', dataIndex: 'mileage', unit: '公里', width: 150 },
      { title: '预计保养里程', dataIndex: 'expectedMileage', width: 110 },
      { title: '最近一次保养里程', dataIndex: 'exportMileage', unit: '公里', width: 180 },
      { title: '保养到期日', dataIndex: 'expectedDate', width: 170 },
      {
        title: '最近一次保养日期',
        dataIndex: 'exportDate',
        sortable: {
          sorter: true,
          sortDirections: ['ascend', 'descend'],
        },
        width: 170,
      },
      { title: '提醒类型', dataIndex: 'warningTypeName', width: 90 },
      {
        title: '提醒时间',
        dataIndex: 'warningTime',
        sortable: {
          sorter: true,
          sortDirections: ['ascend', 'descend'],
        },
        render: (record) => {
          if (record.warningTime) {
            return dayjs(record.warningTime).format('YYYY-MM-DD HH:mm:ss')
          }
        },
        width: 170,
      },
    ],
    buttons: [
      {
        mode: 'import',
        label: '导出',
        permission: 'svms:asstes:car:car-maintenance.export',
        method: () => handleExport(),
      },
    ],
    operates: [
      {
        type: 'primary',
        icon: 'plus',
        label: '新增保养',
        permission: 'svms:asstes:car:car-maintenance.add',
        method: (record) => {
          navigateTo(`/car-maintenance/create/${record.carId}?page=toDo`)
        },
      },
    ],
  })

  // 列表接口调用
  const loadData = useFetchTableDataApi(table, queryParam, getMaintenanceList)

  // modal展示
  const visible = ref(false)
  let pageTlt = ref('')
  const open = (label) => {
    pageTlt = label
    visible.value = true
    loadData(1)
  }
  const onClose = () => {
    visible.value = false
    queryParam.siteId = ''
    queryParam.carSiteId = ''
    queryParam.carId = ''
    queryParam.sortField = ''
    queryParam.sortType = ''
  }

  // 导出事件
  const handleExport = () => {
    useExport(getMaintenanceExport, queryParam)
  }

  defineExpose({
    open,
  })
</script>

<style lang="less" scoped>
  .car-msg {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .car-msg-left {
    margin-right: 10px;
  }
  .col-lic {
    color: #1d2129;
    font-size: 14px;
    font-weight: 600;
    margin-right: 16px;
  }
</style>
